package com.itc.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itc.common.core.domain.R;
import com.itc.common.core.utils.poi.ExcelUtil;
import com.itc.common.core.validate.Add;
import com.itc.common.core.validate.Update;
import com.itc.common.log.annotation.Log;
import com.itc.common.log.enums.BusinessType;
import com.itc.common.security.annotation.RequiresPermissions;
import com.itc.system.api.domain.SysDictType;
import com.itc.system.domain.dto.SysDictTypeDto;
import com.itc.system.service.SysDictTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典类型信息
 * 
 * @author itc
 */
@RestController
@RequestMapping("/dict/type")
@Api(value = "sysDictType", tags = "数据字典类型信息管理")
public class SysDictTypeController
{
    @Autowired
    private SysDictTypeService dictTypeService;

    @RequiresPermissions("system:dict:list")
    @PostMapping("/page")
    @ApiOperation(value = "分页查询字典类型数据")
    public R<Page<SysDictType>> getPage(@RequestBody SysDictType dictType)
    {
        Page<SysDictType> page = new Page<>();
        page.setOptimizeCountSql(true);
        page.setSize(dictType.getSize());
        page.setCurrent(dictType.getCurrent());
        Page<SysDictType> resultPage = dictTypeService.page(page,new LambdaQueryWrapper<>(dictType));
        return R.ok(resultPage);
    }

    @PostMapping("/list")
    @ApiOperation(value = "查询字典类型数据列表")
    public R<List<SysDictType>> getList(@RequestBody SysDictType dictType)
    {
        List<SysDictType> list = dictTypeService.list(new LambdaQueryWrapper<>(dictType));
        return R.ok(list);
    }

    @Log(title = "字典类型", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:dict:export")
    @PostMapping("/export")
    @ApiOperation(value = "字典类型数据导出")
    public void export(HttpServletResponse response,@RequestBody SysDictType dictType)
    {
        List<SysDictType> list = dictTypeService.list(new LambdaQueryWrapper<>(dictType));
        ExcelUtil<SysDictType> util = new ExcelUtil<SysDictType>(SysDictType.class);
        util.exportExcel(response, list, "字典类型");
    }

    /**
     * 查询字典类型详细
     */
    @RequiresPermissions("system:dict:query")
    @GetMapping(value = "/detail/{dictId}")
    @ApiOperation(value = "字典类型详情")
    public R<SysDictType> getDetail(@PathVariable Long dictId)
    {
        return R.ok(dictTypeService.getById(dictId));
    }

    /**
     * 新增字典类型
     */
    @RequiresPermissions("system:dict:add")
    @Log(title = "字典类型", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "字典类型新增")
    public R add(@Validated(Add.class) @RequestBody SysDictTypeDto dict)
    {
        return dictTypeService.insertDictType(dict);
    }

    /**
     * 修改字典类型
     */
    @RequiresPermissions("system:dict:edit")
    @Log(title = "字典类型", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    @ApiOperation(value = "字典类型编辑")
    public R edit(@Validated(Update.class) @RequestBody SysDictTypeDto dict)
    {
        return dictTypeService.updateDictType(dict);
    }

    /**
     * 删除字典类型
     */
    @RequiresPermissions("system:dict:remove")
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{dictIds}")
    @ApiOperation(value = "删除字典类型")
    public R remove(@PathVariable List<Long> dictIds)
    {
        return dictTypeService.deleteDictTypeByIds(dictIds);
    }
}
